package apis

import (
	"github.com/gin-gonic/gin"
	"common/logger"
	"net/http"

	"tylnmall/models"
	"github.com/jinzhu/gorm"
	"errors"
	"tylnmall/constant"
	"fmt"
)

//添加一个赞 帖子
func ZanAddApi(c *gin.Context) {
	type param struct {
		Userid int `json:"userid" form:"userid"`
		Postid int `json:"postid" form:"postid"`
		Type   int `json:"type" form:"type"`
	}
	var err error
	var p param
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	if p.Userid == 0 || p.Postid == 0 {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	var zan models.Zan
	db := c.MustGet("ty").(*gorm.DB)
	var z []*models.Zan
	if err = db.Where("user_id = ? and post_id = ? and type = ?", p.Userid, p.Postid, p.Type).Find(&z).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	if len(z) <= 0 {
		zan.Type = p.Type
		zan.PostId = p.Postid
		zan.UserId = p.Userid
		if err = db.Create(&zan).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
	} else {
		err = errors.New("你已经赞过了")
		logger.Error(err)
		var str = "点赞"
		c.JSON(http.StatusOK, gin.H{"err_code": "你已经" + str, "err_msg": "你已经" + str})
		return
	}
	if p.Type == 0 {
		var post models.Post
		if err = db.Where("id = ?", p.Postid).Find(&post).Error; err != nil {
			err = errors.New("post not found")
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
		//fmt.Println("-------------------")
		//fmt.Println(post.Lastbacktime)
		//fmt.Println(post.Createtime)
		//fmt.Println("-------------------")
		//post.Zan ++
		//dates := strings.Split(strings.Split(post.Lastbacktime, "+")[0], "T")
		//post.Lastbacktime = dates[0] + " " + dates[1]
		//dates = strings.Split(strings.Split(post.Createtime, "+")[0], "T")
		//post.Createtime = dates[0] + " " + dates[1]
		if err = db.Model(&models.Post{}).Where("id = ?",post.Id).Update("zan" ,post.Zan+1).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
	} else {
		var post models.Backpost
		if err = db.Where("id = ?", p.Postid).Find(&post).Error; err != nil {
			err = errors.New("post not found")
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
		post.Zan ++
		fmt.Println(post.Createtime)
		//dates := strings.Split(strings.Split(post.Createtime, "+")[0], "T")
		//post.Createtime = dates[0] + " " + dates[1]
		if err = db.Model(&models.Backpost{}).Where("id = ?",post.Id).Update("zan" ,post.Zan+1).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
	}

	c.JSON(http.StatusOK, gin.H{"status": "success"})
}

//添加一个收藏 tiezi
func CollectionAddApi(c *gin.Context) {
	type param struct {
		Userid int `json:"userid" form:"userid"`
		Postid int `json:"postid" form:"postid"`
	}
	var err error
	var p param
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	if p.Userid == 0 || p.Postid == 0 {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	var coll models.Collection
	db := c.MustGet("ty").(*gorm.DB)
	var z []*models.Collection
	if err = db.Where("user_id = ? and post_id = ?", p.Userid, p.Postid).Find(&z).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	if len(z) <= 0 {
		coll.PostId = p.Postid
		coll.UserId = p.Userid
		if err = db.Create(&coll).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
	} else {
		err = errors.New("你已经收藏了")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": "你已经收藏了", "err_msg": "你已经收藏了"})
		return
	}
	var post models.Post
	if err = db.Where("id = ?", p.Postid).Find(&post).Error; err != nil {
		err = errors.New("post not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	//post.Collection ++
	//dates := strings.Split(strings.Split(post.Lastbacktime, "+")[0], "T")
	//post.Lastbacktime = dates[0] + " " + dates[1]
	//dates = strings.Split(strings.Split(post.Createtime, "+")[0], "T")
	//post.Createtime = dates[0] + " " + dates[1]
	if err = db.Model(&models.Post{}).Where("id = ?",post.Id).Update("collection" ,post.Zan+1).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": "success"})
}

//帖子是否已经被收藏点赞
func IsCollectApi(c *gin.Context) {
	type param struct {
		Userid int `json:"userid" form:"userid"`
		Postid int `json:"postid" form:"postid"`
	}
	var err error
	var p param
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	if p.Userid == 0 || p.Postid == 0 {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	db := c.MustGet("ty").(*gorm.DB)
	var zan []*models.Zan
	if err = db.Where("user_id = ? and post_id = ? and type = 0", p.Userid, p.Postid).Find(&zan).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	var isZan = len(zan)
	var col []*models.Collection
	if err = db.Where("user_id = ? and post_id = ?", p.Userid, p.Postid).Find(&col).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	var isCol = len(col)
	c.JSON(http.StatusOK, gin.H{"isZan": isZan, "isCol": isCol})
}

//取消帖子或回复的点赞
func ZanDelApi(c *gin.Context) {
	type param struct {
		Userid int `json:"userid" form:"userid"`
		Postid int `json:"postid" form:"postid"`
		Type   int `json:"type" form:"type"`
	}
	var err error
	var p param
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	if p.Userid == 0 || p.Postid == 0 {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	var zan models.Zan
	db := c.MustGet("ty").(*gorm.DB)
	var z []*models.Zan
	if err = db.Where("user_id = ? and post_id = ? and type = ?", p.Userid, p.Postid, p.Type).Find(&z).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	if len(z) > 0 {
		if err = db.Where("id = ?", z[0].Id).Delete(&zan).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
	} else {
		err = errors.New("你还没有点赞")
		logger.Error(err)
		var str = "点赞"
		c.JSON(http.StatusOK, gin.H{"err_code": "你还没有" + str, "err_msg": "你还没有" + str})
		return
	}
	if p.Type == 0 {
		var post models.Post
		if err = db.Where("id = ?", p.Postid).Find(&post).Error; err != nil {
			err = errors.New("post not found")
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
		fmt.Println("-------------------")
		fmt.Println(post.Lastbacktime)

		//post.Zan --
		//fmt.Println(post.Zan)
		//fmt.Println("-------------------")
		//dates := strings.Split(strings.Split(post.Lastbacktime, "+")[0], "T")
		//post.Lastbacktime = dates[0] + " " + dates[1]
		//dates = strings.Split(strings.Split(post.Createtime, "+")[0], "T")
		//post.Createtime = dates[0] + " " + dates[1]
		if err = db.Model(&models.Post{}).Where("id = ?", post.Id).Update("zan", post.Zan).Set("zan",post.Zan-1).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
	} else {
		var post models.Backpost
		if err = db.Where("id = ?", p.Postid).Find(&post).Error; err != nil {
			err = errors.New("post not found")
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
		//post.Zan --
		//fmt.Println(post.Createtime)
		//dates := strings.Split(strings.Split(post.Createtime, "+")[0], "T")
		//post.Createtime = dates[0] + " " + dates[1]
		if err = db.Model(&models.Backpost{}).Where("id = ?", post.Id).Update("zan", post.Zan-1).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
	}
	c.JSON(http.StatusOK, gin.H{"status": "success"})
}

//取消帖子的收藏
func CollectionDelApi(c *gin.Context) {
	type param struct {
		Userid int `json:"userid" form:"userid"`
		Postid int `json:"postid" form:"postid"`
	}
	var err error
	var p param
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	if p.Userid == 0 || p.Postid == 0 {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	var coll models.Collection
	db := c.MustGet("ty").(*gorm.DB)
	var z []*models.Collection
	if err = db.Where("user_id = ? and post_id = ?", p.Userid, p.Postid).Find(&z).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	if len(z) > 0 {
		if err = db.Where("id = ?", &z[0].Id).Delete(&coll).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
	} else {
		err = errors.New("你还没有收藏")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": "你还没有收藏了", "err_msg": "你还没有收藏了"})
		return
	}
	var post models.Post
	if err = db.Where("id = ?", p.Postid).Find(&post).Error; err != nil {
		err = errors.New("post not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	//post.Collection --
	//dates := strings.Split(strings.Split(post.Lastbacktime, "+")[0], "T")
	//post.Lastbacktime = dates[0] + " " + dates[1]
	//dates = strings.Split(strings.Split(post.Createtime, "+")[0], "T")
	//post.Createtime = dates[0] + " " + dates[1]
	if err = db.Model(&models.Post{}).Where("id = ?", post.Id).Update("collection",post.Collection-1).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}

	c.JSON(http.StatusOK, gin.H{"status": "success"})
}

//查找所有收藏的帖子
func CollectAllListApi(c *gin.Context) {
	type param struct {
		Id    int `json:"id" form:"id"`
		Limit int `json:"limit" form:"limit"` //每页数量
		Page  int `json:"page" form:"page"`   //页码
	}
	var err error
	var p param
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	if p.Limit <= 0 {
		p.Limit = 10
	}
	if p.Page <= 0 {
		p.Page = 1
	}
	var zan []*models.Collection
	var count float64
	db := c.MustGet("ty").(*gorm.DB)
	if err = db.Limit(p.Limit).Offset((p.Page - 1) * p.Limit).Where("user_id = ? ", p.Id).Find(&zan).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	if err := db.Where("user_id = ? ", p.Id).Model(&models.Collection{}).Count(&count).Error; err != nil {
		logger.Error(err)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}
	type list struct {
		Post     models.Post `json:"post"`
		UserName string      `json:"user_name"` //用户信息
		Image    string      `json:"image"`
	}
	var List []list
	for _, z := range zan {
		var l list
		var user models.User
		var post models.Post
		if err = db.Where("id = ? ", z.PostId).Find(&post).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
		l.Post = post
		if err = db.Where("id = ? ", z.UserId).Find(&user).Error; err != nil {
			logger.Error(err)
			c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
			return
		}
		l.UserName = user.Name
		l.Image = user.Image
		List = append(List, l)
	}
	c.JSON(http.StatusOK, gin.H{"list": &List, "all_num": count})
}
